<template>
  <div id="subpage">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <b>网站设置</b>
        </el-breadcrumb-item>
        <el-breadcrumb-item>首页栏目管理列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
    <div class="content">
      <div class="xcx-head">
        <div style="display: flex">
          <span>首页栏目管理列表</span>
        </div>
        <div style="display: flex">
          <el-button @click="dialogFormVisible = true" size="small" icon="el-icon-plus" type="success">新增栏目</el-button>
        </div>
      </div>
      <div class="search-box mt10">
        <el-form :inline="true">
          <el-form-item>
            <el-input placeholder="请输入关键词" v-model="search.keys"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getIndex()" icon="el-icon-search" type="primary">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button @click="getClearIndex()" icon="el-icon-delete" type="info">清空筛选</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="xcx-content">
        <!--列表-->
        <el-table border :data="tableData" style="width: 100%" row-key="id" size="small" v-loading="loading">
          <el-table-column align="center" prop="id" label="ID" width="100"></el-table-column>
          <el-table-column align="center" prop="title" label="图标" width="80">
            <template #default="scope">
              <el-image style="width: 20px;height: 20px" :src="scope.row.icon" fit="cover" lazy></el-image>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="title" label="栏目名称" width="220"></el-table-column>
          <el-table-column prop="urls" label="连接地址" width="320"></el-table-column>
          <el-table-column></el-table-column>
          <el-table-column align="center" prop="sort" label="排序" width="220"></el-table-column>
          <el-table-column label="操作" width="180">
            <template #default="scope">
              <el-button icon="el-icon-folder-checked" plain type="primary" size="mini"
                @click="edit(scope.row.id)">编辑</el-button>
              <el-button icon="el-icon-delete" plain type="danger" size="mini"
                @click="del_item(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--分页-->
        <div class="paging">
          <el-pagination class="left" @current-change="handleCurrentChange" :current-page="page" background
            layout="total, prev, pager, next" :total="count"></el-pagination>
        </div>

      </div>
    </div>

    <el-dialog title="分类操作" width="36%" :visible.sync="dialogFormVisible" :close-on-click-modal="false"
      @close="closeDialog">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="栏目名称：" :label-width="formLabelWidth" prop="title">
          <el-input v-model="ruleForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标：" :label-width="formLabelWidth" prop="icon">
          <upload-one w="150px" h="100px" v-model="ruleForm.icon"></upload-one>
        </el-form-item>
        <el-form-item label="连接地址：" :label-width="formLabelWidth" prop="urls">
          <el-input v-model="ruleForm.urls" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序：" :label-width="formLabelWidth" prop="sort">
          <el-input v-model="ruleForm.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import UploadOne from "../../components/upload/UploadOne";
export default {
  components: {
    UploadOne,
  },
  data() {
    return {
      dialogFormVisible: false,
      loading: true,
      tableData: [],
      page: 0,
      count: 0,
      limit: 10,
      ruleForm: {
        id: 0,
        title: "",
        icon: "",
        urls: "",
        sort: 0,
      },
      rules: {
        title: [{ required: true, message: "请填写分类", trigger: "blur" }],
        icon: [{ required: true, message: "请上传图标", trigger: "change" }],
        urls: [{ required: true, message: "请填写连接地址", trigger: "blur" }],
      },
      search: {
        keys: "",
      },
      formLabelWidth: "120px",
    };
  },
  //进入页面加载
  mounted: function () {
    var that = this;
    that.getList();
  },
  methods: {
    handleCurrentChange(currentPage) {
      var that = this;
      that.page = currentPage;
      that.getList();
    },

    getIndex() {
      this.getList();
    },

    getClearIndex() {
      this.search.keys = "";
      this.getList();
    },

    del_item(ids) {
      console.log(ids);
      let that = this;
      this.$confirm("此操作不可逆, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        that.axios
          .post("/icons/destroy", {
            ids: ids,
          })
          .then((res) => {
            console.log(res);
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              that.getList();
            } else {
              this.$message({
                type: "info",
                message: res.msg,
              });
            }
          });
      });
    },

    /**
     * 获取列表
     */
    getList() {
      let that = this;
      let formData = that.search;
      var curr_page = sessionStorage.getItem("curr_page");
      that.loading = true;
      that.page = curr_page ? curr_page - 0 : that.page;
      formData.page = that.page;
      formData.limit = that.limit;
      that.axios.post("/icons/index", formData).then((res) => {
        if (res) {
          that.tableData = res.data.data;
          that.count = res.data.total;
          that.page = curr_page ? curr_page : that.page;
          curr_page ? sessionStorage.removeItem("curr_page") : "";
          that.loading = false;
        }
      });
    },

    edit(ids) {
      var that = this;
      that.id = ids;
      that.axios.post("/icons/detail", { id: ids }).then((res) => {
        if (res) {
          that.ruleForm.id = res.data.id;
          that.ruleForm.title = res.data.title;
          that.ruleForm.urls = res.data.urls;
          that.ruleForm.icon = res.data.icon;
          that.ruleForm.sort = res.data.sort;
          that.dialogFormVisible = true;
        }
      });
    },

    /**
     * 提交信息
     */
    submitForm(formName) {
      const that = this;
      that.$refs[formName].validate((valid) => {
        if (!valid) return that.$message.warning("请完整填写内容!");
        var formData = {};
        formData = that.ruleForm;
        var url = "/icons/beforeSave";
        that.axios.post(url, formData).then(() => {
          that.$message.success(that.id ? "修改成功" : "添加成功");
          that.dialogFormVisible = false;
          that.ruleForm.id = 0;
          that.ruleForm.title = "";
          that.ruleForm.urls = "";
          that.ruleForm.icon = "";
          that.ruleForm.sort = 0;
          that.getList();
        });
      });
    },

    closeDialog() {
      const that = this;
      that.ruleForm.id = 0;
      that.ruleForm.title = "";
      that.ruleForm.urls = "";
      that.ruleForm.icon = "";
      that.ruleForm.sort = 0;
    },
  },
};
</script>
<style></style>